package ru.nnstu.server;

import java.io.Serializable;

import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.ObjectMessage;

import javax.jms.Session;
import javax.jms.Topic;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;

public class TopicPublisher {

	private JmsTemplate jmsTemplate;
	private Topic topic1;
	private Topic topic2;
	private static Logger log = Logger.getLogger(TopicPublisher.class);

	public void produce1(final Object object) {
		
		this.jmsTemplate.send(this.topic1, new MessageCreator() {
			public Message createMessage(Session session) throws JMSException {
				ObjectMessage mm = session.createObjectMessage();
				mm.setObject((Serializable) object);
				return mm;
			}
		});
//		log.info("msg sent to 1");

	}

	public void produce2(final Object object) {
		this.jmsTemplate.send(this.topic2, new MessageCreator() {
			public Message createMessage(Session session) throws JMSException {
				ObjectMessage mm = session.createObjectMessage();
				mm.setObject((Serializable) object);
				return mm;
			}
		});
//		log.info("msg sent to 2");
	}

	@Required
	public void setConnectionFactory(ConnectionFactory connectionFactory) {
		this.jmsTemplate = new JmsTemplate(connectionFactory);
	}

	@Required
	public void setTopic1(Topic topic) {
		this.topic1 = topic;
	}

	@Required
	public void setTopic2(Topic topic) {
		this.topic2 = topic;
	}

}